package com.baizhi.service.impl;

import com.baizhi.dao.OrderItemDao;
import com.baizhi.entity.OrderItem;
import com.baizhi.service.OrderItemService;
import com.baizhi.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class OrderItemServiceImpl implements OrderItemService {

    @Override
    public List<OrderItem> selectOrderForOrderItemsById(String orderId) {
        List<OrderItem> orderItems=new ArrayList<>();
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            OrderItemDao orderItemDao = sqlSession.getMapper(OrderItemDao.class);
            orderItems=orderItemDao.selectOrderForOrderItemsById(orderId);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return orderItems;
    }

    @Override
    public void addOrderItem(OrderItem orderItem) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            OrderItemDao orderItemDao = sqlSession.getMapper(OrderItemDao.class);
            orderItemDao.addOrderItem(orderItem);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
    }
}
